﻿/*+========================================================
File:		EnemyRun.cpp
Sumary:		Hiện thực hóa các phương thức của lớp EnemyRun
========================================================+*/

#include "EnemyRun.h"
#include "PlayState.h"

CEnemyRun::CEnemyRun(CBill* pBill)
{
	m_pBill = pBill;
	m_fTimeBorn = 1.0f;
	m_bCanBorn = true;
	m_nCount = 0;
	for (int i = 0; i < MAX_ENEMIES; i++)
	{
		CRunningMan* obj = new CRunningMan(D3DXVECTOR2(0, 0), i, ID_RUNNING_MAN, 0);
		obj->Kill();
		CPlayState::s_enemyRun[i] = obj;
	}
}

CEnemyRun::~CEnemyRun()
{
	if (m_pBill)
		delete m_pBill;
	for (int i = 0; i < MAX_ENEMIES; i++)
	{
		if (CPlayState::s_enemyRun[i])
			delete CPlayState::s_enemyRun[i];
	}
}

void CEnemyRun::Update(float timeFrame, vector<CGameObject*> objectInVP)
{
	if (!m_bCanBorn)
		return;
	if (m_fTimeBorn > 0.0f)
	{
		m_fTimeBorn -= timeFrame;
	}
	else
	{
		//Lấy hình chữ nhật của Viewport
		m_VPPos = CViewport::GetInstance()->GetPosition();
		RECT vpRect = { m_VPPos.x, m_VPPos.y, m_VPPos.x + SCREEN_WIDTH, m_VPPos.y - SCREEN_HEIGHT };
		//Random vị trí
		CGameObject* obj = NULL;
		vector<CGameObject*> grassObject = CEnemyRun::GetAllGrassObject(objectInVP);
		//output_print("%d\n", grassObject.size());
		if (grassObject.size() <= 0)
			return;
		srand(time(NULL));
		int i = rand() % grassObject.size();
		if (grassObject[i]->GetTypeId() == ID_GRASS_CAN_FALL
			|| grassObject[i]->GetTypeId() == ID_GRASS)
		{
			obj = grassObject[i];
			if (m_pBill->IsRight())
			{
				if (obj->GetBox().x < CViewport::GetInstance()->GetPosition().x + SCREEN_WIDTH
					&& obj->GetBox().x + obj->GetBox().width > CViewport::GetInstance()->GetPosition().x + SCREEN_WIDTH)
				{
					if (rand() % 100 < 90)
					{
						D3DXVECTOR2 pos(vpRect.right + 70.0f, obj->GetBox().y + 5.0f);
						if (!CPlayState::s_enemyRun[m_nCount]->IsLive())
							CPlayState::s_enemyRun[m_nCount]->Reset(pos, false);
						m_nCount = (m_nCount + 1) % MAX_ENEMIES;
						m_fTimeBorn = 3.0f;
					}
					/*else
					{
						D3DXVECTOR2 pos(vpRect.left - 60.0f, obj->GetBox().y + 20.0f);
						if (!CPlayState::s_enemyRun[m_nCount]->IsLive())
							CPlayState::s_enemyRun[m_nCount]->Reset(pos, true);
						m_nCount = (m_nCount + 1) % MAX_ENEMIES;
						m_fTimeBorn = 5.0f;
					}*/
				}
			}
			else
			{
				if (obj->GetBox().x < CViewport::GetInstance()->GetPosition().x
					&& obj->GetBox().x + obj->GetBox().width > CViewport::GetInstance()->GetPosition().x)
				{
					if (rand() % 100 < 90)
					{
						D3DXVECTOR2 pos(vpRect.left - 70.0f, obj->GetBox().y + 5.0f);
						if (!CPlayState::s_enemyRun[m_nCount]->IsLive())
							CPlayState::s_enemyRun[m_nCount]->Reset(pos, true);
						m_nCount = (m_nCount + 1) % MAX_ENEMIES;
						m_fTimeBorn = 3.0f;
					}
					/*else
					{
						D3DXVECTOR2 pos(vpRect.right + 60.0f, obj->GetBox().y + 20.0f);
						if (!CPlayState::s_enemyRun[m_nCount]->IsLive())
							CPlayState::s_enemyRun[m_nCount]->Reset(pos, false);
						m_nCount = (m_nCount + 1) % MAX_ENEMIES;
						m_fTimeBorn = 5.0f;
					}*/
				}
			}
		}
	}
}

void CEnemyRun::Destroy()
{
	m_bCanBorn = false;
}

vector<CGameObject*> CEnemyRun::GetAllGrassObject(vector<CGameObject*> objInViewport)
{
	vector<CGameObject*> result;
	Box vpBox = { CViewport::GetInstance()->GetPosition().x, CViewport::GetInstance()->GetPosition().y, 
		SCREEN_WIDTH, SCREEN_HEIGHT, 0.0f, 0.0f };
	for (int i = 0; i < objInViewport.size(); i++)
	{
		if (objInViewport[i]->GetTypeId() == ID_GRASS || objInViewport[i]->GetTypeId() == ID_GRASS_CAN_FALL)
			if (objInViewport[i]->GetBox().IsIntersect(vpBox))
				result.push_back(objInViewport[i]);
	}
	return result;
}